﻿#include "showWidget.h"
#include <QPainterPath>
showWidget::showWidget(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);

    connect(ui.outline, &QPushButton::clicked, this, &showWidget::outlineWay);
    connect(ui.show, &QPushButton::clicked, this, &showWidget::showAllWay);
    connect(ui.return_2, &QPushButton::clicked, this, &showWidget::back);
    connect(ui.find, &QPushButton::clicked, this, &showWidget::find);

    //设置表格自适应宽度 
    ui.Table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);               //x先自适应宽度
    ui.Table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);     //然后设置要根据内容使用宽度的列
    ui.Table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);     //然后设置要根据内容使用宽度的列
    // 设置表格不可编辑
    ui.Table->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui.accountingTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    //窗口图标
    this->setWindowIcon(QIcon("窗口，表单.png"));
    this->setWindowTitle("展示");
    //鼠标光标
    QPixmap pixmap = QPixmap("光标.png");
    pixmap = pixmap.scaled(20, 20, Qt::KeepAspectRatio);
    QCursor cursor = QCursor(pixmap);
    setCursor(cursor);
}

showWidget::~showWidget()
{

}
// 绘制圆
void showWidget::myPaint(QPainter& painter, int x, int y, quint8 n) {
    QTransform transform;
    transform.translate(x, y);
    // 旋转绘制圆
    for (int i = 0; i < n; i++) {
        transform.rotate(360 / n);
        painter.setTransform(transform);
        painter.drawEllipse(QPoint(300, 0), 30, 30);
        painter.drawText(QPoint(335, 0), this->m.vex[i]);
    }
}
// 绘制箭头
void showWidget::paintArrow(QPainter& painter, QPointF start, QPointF end,QPen pen,int i,int j, QPointF(* interPoints)[25]) {
    //设置 画笔
    painter.setPen(pen);
    // 直线
    QLineF line(start, end);
    // 箭头三角
    QPointF controlPoint = (start + end) / 2 + interPoints[i][j];
    QPainterPath path(start);
    //QPointF controlPoint((start.x() + end.x()) / 2, (start.y() + end.y()) / 2 - 50);
    path.quadTo(controlPoint, end); // 二次贝塞尔曲线，控制点决定弯曲程度
   

    QPointF arrowP1, arrowP2;
    double angel = std::atan2(line.dx(),line.dy());
    end = end + QPointF(sin(angel), cos(angel) );
    QLineF line2(start, end);
    painter.drawPath(path);        // 绘制路径
    //painter.drawLine(line2);
    arrowP1 = line2.p2() + QPointF(sin(angel + 4 * M_PI / 5) * 10, cos(angel + 4 * M_PI / 5) * 10);
    arrowP2 = line2.p2() + QPointF(sin(angel - 4 * M_PI / 5) * 10, cos(angel - 4 * M_PI / 5) * 10);
    QPolygonF arrowHead;
    arrowHead << line2.p2() << arrowP1 << arrowP2;
    //painter.setBrush(pen.color());
    painter.drawPolygon(arrowHead);

}
// 绘制数值
void showWidget::drawNum(QPainter& painter,QPointF start,QPointF end ,int i,int j, int(*a)[25], QPointF(*interPoints)[25]) {
    //QPointF controlPoint = (start + end) / 2 + interPoints[i][j];
    //QPointF startTangent = start + 2 * (controlPoint - start);
    //QPointF endTangent = end + 2 * (end - controlPoint);
    //QPointF midPoint = -0.75 * start + 0.5 * controlPoint + 1.25 * end;
    QLineF line(start, end);
    // 显示数值
    QPointF pointCenter = (start + end) / 2;
    // QLineF line3(QPoint(0, 0), pointCenter);
    // 计算角度
    double rotateAngel = std::atan2(pointCenter.y(), pointCenter.x());
    rotateAngel = rotateAngel / M_PI * 180;
    painter.rotate(rotateAngel);
    // 计算距离
    double len = sqrt(300 * 300 - (line.length() / 2) * (line.length() / 2));
    // 设置字体
    QFont font("幼圆", 15, QFont::Bold, true);
    painter.setFont(font);
    // 绘制文本
    painter.drawText(QPoint(len  + 10 * a[i][j]-20, 100 * a[i][j]), QString::number(this->m.edge[i][j]));
    painter.rotate(-rotateAngel);
}
// 凸显路径
void showWidget::highlight(QPainter& painter, QPointF start, QPointF end,QPen pen, int i,int j,QPointF(* interPoints)[25]) {
    pen.setWidth(2);
    paintArrow(painter, start, end, pen,i,j, interPoints);
}
// 寻找路径
void showWidget::dfs( QVector<bool>& visited, QVector<int>& path, int start, int current, double product, Result& result,int& index,bool &flag) {

    if (start == current && visited[current]) {
        path.push_back(current);
        if (product > 1.0) {
            QString strItem = "";
            for (int i = 0; i < path.size(); ++i) {
                if (strItem != "") {
                    strItem = strItem + "→" + this->m.vex[path[i]];
                }
                else {
                    strItem = this->m.vex[path[i]];
                }
            }
            if (ui.Table->rowCount() < index + 1)    ui.Table->insertRow(ui.Table->rowCount());
            QTableWidgetItem* item = new QTableWidgetItem(strItem);
            ui.Table->setItem(index, 0, item);
            QTableWidgetItem* item2 = new QTableWidgetItem();
            item2->setData(Qt::DisplayRole, product);
            ui.Table->setItem(index, 1, item2);
            item->setTextAlignment(Qt::AlignCenter);
            item2->setTextAlignment(Qt::AlignCenter);
            index++;
        }
        if (product > result.max_product) {
            result.max_product = product;
            result.max_path = path;
        }
        flag = true;
        return;
    }

    visited[current] = true;
    path.push_back(current);

  for (int i = 0; i < this->m.getVexNum(); ++i) {
      if ((!visited[i] || (visited[i] && i == start && i != current)) && this->m.edge[current][i] > 0) { // 检查有效边
          dfs(visited, path, start, i, product * this->m.edge[current][i], result, index,flag);
          path.pop_back();
          if (i != start)visited[i] = false;
      }
  }
}
// 设置要凸显的路径
void showWidget::outlineWay() {

    this->key = true;
    // 先重置
    for (int i = 0; i < this->m.getVexNum(); i++) {
        for (int j = 0; j < this->m.getVexNum(); j++) {
            arr[i][j] = false;
        }
    }
    // 设置凸显的路径
    if (ui.Table->currentItem() != NULL) {
        QString wayStr = ui.Table->currentItem()->text();
        QStringList wayStrList = wayStr.split("→");
        if (wayStrList.size() <= 1) {
            return;
        }
        int currency[25], i;
        for (i = 0; i < wayStrList.size(); i++) {
            for (int j = 0; j < this->m.getVexNum(); j++) {
                if (wayStrList[i] == this->m.vex[j]) {
                    currency[i] = j;
                }
            }
        }
        currency[i] = currency[0];
        for (i = 0; i < wayStrList.size(); i++) {
            this->arr[currency[i]][currency[i + 1]] = true;
        }
        // 重绘
        this->update();
    }
}
// 展示所有路径
void showWidget::showAllWay() {

    this->key = false;
    // 重置
    for (int i = 0; i < this->m.getVexNum(); i++) {
        for (int j = 0; j < this->m.getVexNum(); j++) {
            arr[i][j] = false;
        }
    }
    QString str = ui.lineEdit->text();
    for (int i = 0; i < ui.Table->rowCount(); i++) {
        ui.Table->setRowHidden(i,false);
    }
    //重绘
    this->update();
}
// 返回
void showWidget::back() {
    arbitrage* interface = new arbitrage();
    this->close();
    interface->show();
}
// 查找
void showWidget::find() {
    QString str = ui.lineEdit->text();
    for (int i = 0; i < ui.Table->rowCount(); i++) {
        if (ui.Table->item(i,0)!=nullptr && !ui.Table->item(i, 0)->text().contains(str)) {
            ui.Table->setRowHidden(i, true);
        }
    }
}